<script setup>
  import { ref, onMounted } from 'vue'
  import messageApi from '@/apis/message'
  const notifityList = ref([])
  const isEmpty = ref(false)
  const nextPage = ref(1)
  const hasMore = ref(true)
  const isTriggered = ref(false)
  // 组件中建议使用vue生命周期
  onMounted(() => {
    getNotifyList()
  })
  function onScrolltolower() {
    if (!hasMore.value) return
    getNotifyList(nextPage.value)
  }
  // 监听用户的下拉操作
  async function onScrollRefresh() {
    isTriggered.value = true
    await getNotifyList()
    isTriggered.value = false
  }
  async function getNotifyList(page = 1, pageSize = 5) {
    const { code, data } = await messageApi.list(201, page, pageSize)
    if (code !== 200) return uni.utils.toast('获取列表失败！')
    if (page === 1) notifityList.value = []
    notifityList.value = [...notifityList.value, ...data.items]
    isEmpty.value = notifityList.value.length === 0
    nextPage.value = ++data.page
    hasMore.value = nextPage.value <= data.pages
  }
</script>
<template>
  <scroll-view
    @scrolltolower="onScrolltolower"
    @refresherrefresh="onScrollRefresh"
    :refresher-triggered="isTriggered"
    class="scroll-view"
    refresher-enabled
    scroll-y
  >
    <view class="scroll-view-wrapper">
      <view class="message-action">
        <text class="iconfont icon-clear"></text>
        全部已读
      </view>
      <uni-card
        v-for="notifity in notifityList"
        :key="notifity.id"
        :border="false"
        :is-shadow="false"
      >
        <view class="brief">{{ notifity.content }}</view>
        <view class="extra">
          <text class="time">{{ notifity.created }}</text>
          <navigator
            hover-class="none"
            class="link"
            :url="`/subpkg_task/detail/index?id=${notifity.id}`"
            >查看详情</navigator
          >
        </view>
        <template v-slot:title>
          <view :class="{ unread: !notifity.isRead }" class="title"
            >您有新的运输任务</view
          >
        </template>
      </uni-card>
      <view v-if="isEmpty" class="message-blank">暂无消息</view>
    </view>
  </scroll-view>
</template>

<style lang="scss" scoped>
  @import './styles.scss';
</style>
